package Controller.admin.manage;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Controller.admin.AdminBaseServlet;
import Entity.User;
import Entity.User;
import Services.WebService;

import java.util.ArrayList;

public class MemberServlet extends AdminBaseServlet {
	@Override
	protected void processRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws ServletException,
			IOException {
		if (action == null || action.equals("list")) {
			this.listUsers(request, response, webServices, action);
		} else if (action.equals("new")) {
			this.newUser(request, response, webServices, action);
		} else if (action.equals("show")) {
			this.showUser(request, response, webServices, action);
		} else if (action.equals("edit")) {
			this.editUser(request, response, webServices, action);
		} else if (action.equals("delete")) {
			this.deleteUser(request, response, webServices, action);
		}
	}

	private void newUser(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws IOException {
		String submit = request.getParameter("submit");
		if(submit!= null && submit.equals("true")){
			//Not done check pws ==
			
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String salt = request.getParameter("salt");
			String email = request.getParameter("email");
			
			String temp = request.getParameter("telephone");
			boolean isTelephoneNotInt = !WebService.isInteger(temp);
			int telephone = (temp == null || isTelephoneNotInt)?-1:Integer.parseInt(temp);
			
			String role = request.getParameter("role");
			
			temp = request.getParameter("loyaltyPoint");
			boolean isLoyaltyPointNotInt = !WebService.isInteger(temp);
			int loyaltyPoint = (temp == null || isLoyaltyPointNotInt)?-1:Integer.parseInt(temp);
	
			User user = new User();
			
			if(email != null)
				user.setEmail(email);
			
			if(loyaltyPoint > 0)
				user.setLoyaltyPoint(loyaltyPoint);

			if(password != null) 
				user.setPassword(password);
			
			if(role != null)
				user.setRole(role);
				
			if(salt != null) 
				user.setSalt(salt);
			
			if(username != null)
				user.setUsername(username);
			
			if(telephone > 0)
				user.setTelephone(telephone);
			
			
			if (request.getMethod() == "POST" && !isTelephoneNotInt && !isLoyaltyPointNotInt && username != null && password != null && salt != null && email != null
					&& telephone > 0 && role != null && loyaltyPoint > 0) {
	
				user = webServices.getEntityManager().newUser(user);
				if (user!=null && user.getId()>0) {
					response.sendRedirect(webServices.getBaseURL()+"/admin/manage/member?action=show&id="+user.getId());
				}
			} else {
				request.setAttribute("user", user);
				request.setAttribute("isTelephoneNotInt", isTelephoneNotInt);
				request.setAttribute("isLoyaltyPointNotInt", isLoyaltyPointNotInt);
				
				action = "failed";
				this.forward(request, response, webServices, action, "/admin/manage/member/new.jsp");
			}
		} else {
			this.forward(request, response, webServices, action, "/admin/manage/member/new.jsp");
		}
	}

	private void listUsers(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		ArrayList<User> userList = webServices.getEntityManager().getUsers();
		request.setAttribute("users", userList);
		this.forward(request, response, webServices, action, "/admin/manage/member/list.jsp");
	}

	private void showUser(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		int userID = Integer.valueOf(request.getParameter("userID"));
		User user = webServices.getEntityManager().getUser(userID);

		request.setAttribute("user", user);
		this.forward(request, response, webServices, action, "/admin/manage/member/list.idv.jsp");
	}

	private void editUser(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws IOException {
		String temp = request.getParameter("id");
		boolean isIDNotInt = !WebService.isInteger(temp);
		int userID = (temp == null || isIDNotInt)?-1:Integer.parseInt(temp);
		
		String submit = request.getParameter("submit");
		if(submit!= null && submit.equals("true")){
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String salt = request.getParameter("salt");
			String email = request.getParameter("email");
			
			temp = request.getParameter("telephone");
			boolean isTelephoneNotInt = !WebService.isInteger(temp);
			int telephone = (temp == null || isTelephoneNotInt)?-1:Integer.parseInt(temp);
			
			String role = request.getParameter("role");
			
			temp = request.getParameter("loyaltyPoint");
			boolean isLoyaltyPointNotInt = !WebService.isInteger(temp);
			int loyaltyPoint = (temp == null || isLoyaltyPointNotInt)?-1:Integer.parseInt(temp);
	
			User user = new User();
			
			if(email != null)
				user.setEmail(email);
			
			if(loyaltyPoint > 0)
				user.setLoyaltyPoint(loyaltyPoint);

			if(password != null) 
				user.setPassword(password);
			
			if(role != null)
				user.setRole(role);
				
			if(salt != null) 
				user.setSalt(salt);
			
			if(username != null)
				user.setUsername(username);
			
			if(telephone > 0)
				user.setTelephone(telephone);
			
			
			if (request.getMethod() == "POST" && !isTelephoneNotInt && !isLoyaltyPointNotInt && username != null && password != null && salt != null && email != null
					&& telephone > 0 && role != null && loyaltyPoint > 0) {
	
				webServices.getEntityManager().updateUser(user);
				response.sendRedirect(webServices.getBaseURL()+"/admin/manage/member?action=show&id="+user.getId());
			} else {
				request.setAttribute("user", user);
				request.setAttribute("isTelephoneNotInt", isTelephoneNotInt);
				request.setAttribute("isLoyaltyPointNotInt", isLoyaltyPointNotInt);
				
				action = "failed";
				this.forward(request, response, webServices, action, "/admin/manage/member/edit.jsp");
			}
		} else {
			User user = webServices.getEntityManager().getUser(userID);
			request.setAttribute("user", user);
			this.forward(request, response, webServices, action, "/admin/manage/member/edit.jsp");
		}
	}

	private void deleteUser(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		String temp = request.getParameter("id");
		boolean isIDNotInt = !WebService.isInteger(temp);
		int userID = (temp == null || isIDNotInt)?-1:Integer.parseInt(temp);

		User user = new User();
			String submit = request.getParameter("submit");
		if(submit!= null && submit.equals("true") && request.getMethod() == "POST" && !isIDNotInt && userID > 0) {
			user.setId(userID);
			
			webServices.getEntityManager().deleteUser(user);

			action = "done"; 
			this.forward(request, response, webServices, action, "/admin/manage/member/delete.jsp");
		} else {
			user = webServices.getEntityManager().getUser(userID);
			
			request.setAttribute("user", user);
			this.forward(request, response, webServices, action, "/admin/manage/member/delete.jsp");
		}
	}
}
